﻿<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
	#foreach($c in $ColumnList)
	#if($c.IsPrimary)
	#set ($primaryKeyAlias=$c.ColumnNameAlias)
	#set ($primaryKey=$c.ColumnName)
	#end
    #end
<mapper namespace="${Package}.dao.ext.${table.TableNameAlias}Dao">
	<resultMap type="${Package}.model.${table.TableNameAlias}" id="${table.TableNameAlias}Map">
	#foreach($c in $ColumnList)
		#if(${c.ColumnNameAlias}!=$primaryKeyAlias)
		<result column="${c.ColumnName}" property="${Utils.ToTitleLower(${c.ColumnNameAlias})}" />
		#else
			<id column="${c.ColumnName}" property="${Utils.ToTitleLower(${c.ColumnNameAlias})}" />
		#end
	#end 
	</resultMap>
	<!--插入数据-->
    <insert id="insertByModel" useGeneratedKeys="true" keyProperty="id">
		insert into ${table.TableName}
		 <trim prefix="(" suffix=")" suffixOverrides=",">  
				#foreach($c in $ColumnList)
				#set($columnName=${Utils.ToTitleLower(${c.ColumnNameAlias})})
				   #if(${c.ColumnNameAlias}!=$primaryKeyAlias)
				   <if test="$columnName!=null">
				    $c.ColumnName,
					</if>
				   #end
				#end 
        </trim>  
		values
		<trim prefix="(" suffix=")" suffixOverrides=",">  
			#foreach($c in $ColumnList)
				#if(${c.ColumnNameAlias}!=$primaryKeyAlias)
					#set($columnName=${Utils.ToTitleLower(${c.ColumnNameAlias})})
					 <if test="$columnName!=null">
					#{$columnName},
					</if>
				#end
			#end 
        </trim> 
    </insert>
	    <update id="editByModel" parameterType="${Package}.model.${table.TableNameAlias}">
		update ${table.TableName} 
		<set>
		#foreach($c in $ColumnList)
		#set($columnName=${Utils.ToTitleLower(${c.ColumnNameAlias})})
			#if(${c.ColumnNameAlias}!=$primaryKeyAlias)
			  #if(${c.ColumnNameAlias}!="CreateDate")
			    <if test="$columnName!=null">
			     ${c.ColumnName}=#{$columnName},
				 </if>
			  #end
            #end
		#end
		</set>
		where $primaryKey=#{${Utils.ToTitleLower($primaryKeyAlias)}}
    </update>
	    <select id="selectByMap" parameterType="java.util.Map"
            resultMap="${table.TableNameAlias}Map">
        select * from ${table.TableName} 
        <where>
            <if test="status!=null and status!=-2014">
                and status=#{status}
            </if>
            <if test="status==-2014">
                and status>-1
            </if>
            <if test="startTime!=null">
                and update_date>=#{startTime}
            </if>
            <if test="endTime!=null">
                and update_date>=#{endTime}
            </if>
            <if test="${Utils.ToTitleLower($primaryKeyAlias)}!=null">
                and id=#{${Utils.ToTitleLower($primaryKeyAlias)}}
            </if>
            <if test="key!=null">
                and Enum_Name like "%"#{key}"%"
            </if>
        </where>
        order by sort desc,$primaryKey desc
    </select>
    <select id="count" parameterType="java.util.Map"
            resultType="int">
        select count(0) from ${table.TableName} 
        <where>
            <if test="status!=null and status!=-2014">
                and status=#{status}
            </if>
            <if test="status==-2014">
                and status>-1
            </if>
            <if test="${Utils.ToTitleLower($primaryKeyAlias)}!=null">
                and $primaryKey=#{${Utils.ToTitleLower($primaryKeyAlias)}}
            </if>
            <if test="startTime!=null">
                and update_date>=#{startTime}
            </if>
            <if test="endTime!=null">
                and update_date>=#{endTime}
            </if>
            <if test="key!=null">
                and Enum_Name like "%"#{key}"%"
            </if>
        </where>
    </select>
	<select id="selectByModel" parameterType="${Package}.model.${table.TableNameAlias}"
            resultMap="${table.TableNameAlias}Map">
        select * from
        ${table.TableName} 
        <where>
            <if test="status!=null">
                and status=#{status}
            </if>
        </where>
        order by sort desc,$primaryKey desc
    </select>
    <update id="audit" parameterType="java.util.Map">
        update ${table.TableName}  set status=#{state} where $primaryKey in
        <foreach item="idItem" collection="idArr" open="(" separator="," close=")">
            #{idItem}
        </foreach>
    </update>
    <update id="sort" parameterType="java.util.Map">
        update ${table.TableName}  set sort=#{sort} where $primaryKey=#{${Utils.ToTitleLower($primaryKeyAlias)}}
    </update>
    <delete id="delByMap" parameterType="java.util.Map">
    	delete from ${table.TableName} where status=-1 and $primaryKey in
        <foreach item="idItem" collection="idArr" open="(" separator="," close=")">
            #{idItem}
        </foreach>
    </delete>
</mapper>